package org.trackcore.handler;

import org.trackcore.model.entity.ErrorResource;
import org.trackcore.service.ErrorResourceService;
import org.trackcore.utils.DataUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class ResourceErrorHandler implements DataHandler {
    private final ErrorResourceService errorResourceService;

    public ResourceErrorHandler(ErrorResourceService errorResourceService) {
        this.errorResourceService = errorResourceService;
    }

    @Override
    public String getHandlerKey() {
        return "error_resouce"; // 注意与前端subType保持一致
    }

    @Override
    public void handle(Map<String, Object> data, String logId) {
        ErrorResource error = ErrorResource.builder()
                .logId(logId)
                .url(DataUtils.getString(data, "url"))
                .pageUrl(DataUtils.getString(data, "pageUrl"))
                .html(DataUtils.getString(data, "html"))
                .path(DataUtils.getString(data, "path"))
                .build();

        errorResourceService.save(error);
    }
}
